MyDesktop.ImportWindow = Ext.extend(Ext.app.Module, {
    id:'import-win',
    init : function(){
        this.launcher = {
            text: '安装导入',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },

	record : Ext.data.Record.create([
        	{name : 'id',mapping : 'id',type : 'int'}, 
        	{name : 'pid',mapping : 'fid',type : 'int'}, 
        	{name : 'buyer',mapping : 'buyer',type : 'string'}, 
        	{name : 'unit',mapping : 'unit',type : 'string'}, 
        	{name : 'mobile',mapping : 'mobile',type : 'string'}, 
        	{name : 'phone',mapping : 'phone',type : 'string'}, 
        	{name : 'address',mapping : 'address',type : 'string'}, 
        	{name : 'status',mapping : 'status',type : 'string'},
        	{name : 'memo',mapping : 'memo',type : 'string'}
	]),
	
	
    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('import-win');
		
        if(!win){
		
		var store=new Ext.data.JsonStore({
	      root:'root',
		  fields:[
		    {name : 'id',mapping : 'id',type : 'int'}, 
        	{name : 'pid',mapping : 'fid',type : 'int'}, 
        	{name : 'buyer',mapping : 'buyer',type : 'string'}, 
        	{name : 'partnerId',mapping : 'partnerId',type : 'int'}, 
        	{name : 'mobile',mapping : 'mobile',type : 'string'}, 
        	{name : 'phone',mapping : 'phone',type : 'string'}, 
			{name : 'districtId',mapping : 'districtId',type : 'int'},
        	{name : 'address',mapping : 'address',type : 'string'}, 
        	{name : 'status',mapping : 'status',type : 'string'},
        	{name : 'memo',mapping : 'memo',type : 'string'},
			{name : 'invoicesType',mapping : 'invoicesType',type : 'int'}
		  ]
	    }); 
		

        var fp = new Ext.FormPanel({
            fileUpload: true,
            frame: true,
            autoHeight: true,
            bodyStyle: 'padding: 10px 10px 0 10px;',
            labelWidth: 50,
            defaults: {
                anchor: '95%',
                allowBlank: false,
                msgTarget: 'side'
            },
            items: [{
                xtype: 'fileuploadfield',
                emptyText: '选择一个导入文件',
                fieldLabel: 'Excel',
                name: 'file',
				buttonText: '',
                buttonCfg: {
                    iconCls: 'upload-icon'
                },
                listeners : {
				'fileselected' : function(fb, v) {
					if (Ext.util.Format.lowercase(v).lastIndexOf(".xls") == -1) {
						Ext.Msg.alert("错误", "请选择正确的Excel文件");
						fp.getForm().reset();
					}
				}
			}
            }],
            buttons: [{
                text: '导入',
                handler: function(){
                    if(fp.getForm().isValid()){
    	                fp.getForm().submit({
    	                	waitTitle : '请稍候',
    	                    url: app.url + '/admin/excelUpload.do',
    	                    method :"post",	
    	                    waitMsg: '正在导入',
    	                    success: function(fp, o){
    	                       store.loadData(o.result);
    	                    }
    	                });
                    }
                }
            },{
            text: '重选',
            handler: function(){
                fp.getForm().reset();
            }
        }]
        });
		

	 var combo_partner=getPartnerCombo();
	 var combo_district=getDistrictCombo();
	 var cm=new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), 
		 {header: '销售单位',dataIndex: 'partnerId',editor:combo_partner,renderer:Ext.util.Format.comboRenderer(combo_partner)},
		 {header : '业务单位',width : 120,dataIndex : 'unit',
		  editor: new Ext.form.TextField({
                  
                })
		 },
		 {header : '用户姓名',width : 120,dataIndex : 'buyer',
		   editor: new Ext.form.TextField({
                    allowBlank: false
                })
		 },
		 {header : '手机',width : 90,dataIndex : 'mobile',
		  editor: new Ext.form.TextField({
                    allowBlank: false
                })
		 },
		 {header : '联系电话',width : 80,dataIndex : 'phone',
		  editor: new Ext.form.TextField({
                })
		 },
		 {header : '地区',width : 60,dataIndex : 'districtId',editor:combo_district,renderer:Ext.util.Format.comboRenderer(combo_district)},
		 {header : '用户地址',width : 90,dataIndex : 'address',
		  editor: new Ext.form.TextField({
                    allowBlank: false
                })
		 },
		 {header : '单据日期',width : 90,dataIndex : 'closedate'},
		 {header : '送货日期',width : 90,dataIndex : 'closedate'},
		 {header : '安装日期',width : 80,dataIndex : 'csum'},
		 {header : '级别',width : 60,dataIndex : 'invoicesType',editor:combo_invoicesType_grid,renderer:Ext.util.Format.comboRenderer(combo_invoicesType_grid)}

	]);
		
	   var grid_debit = new Ext.grid.EditorGridPanel({
        	title : '导入数据核对',
        	iconCls : 'icon-grid',
        	collapsible: false,
			border:false,
            region: 'center',
            margins: '5 0 0 0',
			columnLines :true,
			autoScroll : true,
			width : 600,
			enableHdMenu : false,
        	viewConfig:
        	{
        	 forceFit:false,
        	 scrollOffset:15
        	 },	   	   
        	autoWidth : true,
        	region : 'center',
        	cm : cm,
        	ds : store,
        	sm : new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
        	enableColumnMove : false,
        	trackMouseOver : false,
        	frame : true,
			clicksToEdit: 1
        	
        });	
		

		
		var debit_tabs=new Ext.Panel({
            defaults:{autoHeight: true},
            frame:true,
    		collapsible: true,
    		region: 'north',
            height: 130,
            minSize: 100,
            maxSize: 150,
            items : fp

        });
		
		 function submitGrid(){

				var totalRecords =store.getRange();
				var buyItem = [];
				for(var i=0;i<totalRecords.length;i++){
				   
				    buyItem.push(totalRecords[i].data);
				
				}

				
					Ext.Ajax.request({
	           			url:app.url+'/admin/fix/saveImport.do',
	           			method :"post",
						jsonData :buyItem,
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							
							Ext.MessageBox.alert("提示","操作成功!");
							
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
				
		}
		
            win = desktop.createWindow({
                id: 'import-win',
                title:'安装导入',
                width:app.maxWidth,
                height:app.maxHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'border',
                items:[grid_debit,debit_tabs],
                buttonAlign : 'center',
                buttons: [{
                    text:'保存',
					iconCls : 'save',
					handler: submitGrid
                },{
                    text: '取消',
                    handler: function(){
                      //  addInstall_win.hide();
                    }
                }]    
            });
        }
        win.show();
    }
});